<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace AlibabaCloud\Client\Regions;

class LocationService
{
    protected $request;
    protected static $hosts = [];
    const GLOBAL_REGION = "global";
    private function __construct(\AlibabaCloud\Client\Request\Request $request)
    {
        $this->request = $request;
    }
    public static function findProductDomain(\AlibabaCloud\Client\Request\Request $request, $domain = "location.aliyuncs.com")
    {
        return self::resolveHost($request, $domain);
    }
    public static function addEndPoint($regionId, $product, $domain)
    {
        self::addHost($product, $domain, $regionId);
    }
    public static function resolveHost(\AlibabaCloud\Client\Request\Request $request, $domain = "location.aliyuncs.com")
    {
        $locationService = new static($request);
        $product = $locationService->request->product;
        $regionId = $locationService->request->realRegionId();
        if(!isset(self::$hosts[$product][$regionId])) {
            self::$hosts[$product][$regionId] = self::getResult($locationService, $domain);
        }
        return self::$hosts[$product][$regionId];
    }
    private static function getResult($locationService, $domain)
    {
        $locationRequest = new LocationServiceRequest($locationService->request, $domain);
        $result = $locationRequest->request();
        if(!isset($result["Endpoints"]["Endpoint"][0]["Endpoint"])) {
            throw new \AlibabaCloud\Client\Exception\ClientException("Not found Region ID in " . $domain, \AlibabaCloud\Client\SDK::INVALID_REGION_ID);
        }
        return $result["Endpoints"]["Endpoint"][0]["Endpoint"];
    }
    public static function addHost($product, $host, $regionId = self::GLOBAL_REGION)
    {
        \AlibabaCloud\Client\Filter\ApiFilter::product($product);
        \AlibabaCloud\Client\Filter\HttpFilter::host($host);
        \AlibabaCloud\Client\Filter\ClientFilter::regionId($regionId);
        self::$hosts[$product][$regionId] = $host;
    }
    public static function updateEndpoints()
    {
        $ossUrl = "https://openapi-endpoints.oss-cn-hangzhou.aliyuncs.com/endpoints.json";
        $json = file_get_contents($ossUrl);
        $list = json_decode($json, true);
        foreach ($list["endpoints"] as $endpoint) {
            \AlibabaCloud\Client\Config\Config::set("endpoints." . $endpoint["service"] . "." . $endpoint["regionid"], strtolower($endpoint["endpoint"]));
        }
    }
}

?>